<?php

// content="text/plain; charset=utf-8"
require_once ('../jpgraph/jpgraph.php');
require_once ('../jpgraph/jpgraph_bar.php');
require_once ('../jpgraph/jpgraph_canvas.php');
require_once ('../dto/SaisonSelection.php');
require_once ('../util/Validation.php');
require_once ('../dao/SaisonDAO.php');
require_once ('../dao/database.php');
require_once ('../dao/user_db.php');
require_once ('../dao/SportartDAO.php');
require_once ('../dto/SportartSelection.php');

$sportarten = SportartDAO::getAllSportarten();
$sportartSelection = new SportartSelection($sportarten);

if (isset($_GET['saison'])) {
    $saisonIndex = $_GET['saison'];
    if (is_numeric($saisonIndex)){
        $saisons = SaisonDAO::getSaisons();
        $saisonSelect = new SaisonSelection($saisons);
        $saisonSelect->setSelectedIndex(intval($saisonIndex));
    }
    /**
     * GET resultSize
     */
    if (isset($_GET['size'])) {
        $sizeUnsave = $_GET['size'];
        if (is_numeric($sizeUnsave)){
            $size = intval($sizeUnsave);
            if ($size > 200) {
                $size = 200;
            }
        }
    } else {
        $size = 5;
    }
    
    if (isset($_GET['tab'])) {
        $nextTab = $_GET['tab'];
        if (is_numeric($nextTab)){
            if (intval($nextTab) >= 1 && intval($nextTab) <= 4){
                $sportartSelection->setSelectedIndex($nextTab);

                    //hole ergebnis aus db für tab
            } else {
                $nextTab = 0;
            }
        } else {
            $nextTab = 0;
        }
    } else {
        $nextTab = 0;
    }
    
    $best = get_best_users(0, $size, $saisonSelect->getSelectedSaisons(),$sportartSelection->getSelectedIndex());
}


if (count($best['datas']) > 0) {

    $datay = $best['datas']; //array(56, 31, 0, 0, 1, 120);
    // Create the graph. These two calls are always required
    $graph = new Graph(550, 50*$size, 'auto');
    $graph->SetScale("textlin");

    $theme_class = new UniversalTheme;
    $graph->SetTheme($theme_class);

    $graph->Set90AndMargin(100, 20, 20, 10);
    $graph->img->SetAngle(90);

    // set major and minor tick positions manually
    $graph->SetBox(true);

    //$graph->ygrid->SetColor('gray');
    $graph->ygrid->Show(true);
    //    $graph->ygrid->SetFill(true);
    $graph->xaxis->SetTickLabels($best['users']); //array('bierradler', 'babis', 'C', 'D', 'E', 'F'));
    $graph->yaxis->HideLine(false);
    $graph->yaxis->HideTicks(false, false);
    //$graph->box_color('silver'); 
    //    $graph->SetScale("textlin");
    // Add a drop shadow
    //    $graph->SetShadow();
    // For background to be gradient, setfill is needed first.
    
    /**
     * Change the color of background
     */
    $graph->SetMarginColor('#f5f5f5'); 
    $graph->SetFrame(true,'#f5f5f5',1);
    
    
    // Create the bar plots
    $b1plot = new BarPlot($datay);
    //$b1plot->Set3D();
    // Center the values in the bar
    $b1plot->value->Show();
    $b1plot->value->SetFormat('%d');
    $b1plot->value->SetFont(FF_FONT1, FS_BOLD);

    $b1plot->SetValuePos('center');
    $b1plot->SetWeight(0);

    //$b1plot->SetFillGradient("#808000", "#90EE90", GRAD_HOR);
    $b1plot->SetWidth(17);


    // Create the grouped bar plot
    $abplot = new AccBarPlot(array($b1plot));
    //$abplot->SetShadow();
    $abplot->value->Show();
    $abplot->value->SetFont(FF_DEFAULT, FS_BOLD, 10);
    $abplot->value->SetAngle(45);
    $abplot->value->SetFormat('%0.1f');
    $abplot->SetWidth(17);

    //    $abplot->SetValuePos('center');
    // ...and add it to the graPH
    $graph->Add($abplot);



    // Display the graph
    $graph->Stroke();
} else {
    // Create the graph. 
    $graph = new CanvasGraph(350, 200);

    $t1 = new Text("Derzeit keine Daten vorhanden.");
    $t1->SetPos(0.05, 0.5);
    $t1->SetOrientation("h");
    $t1->SetFont(FF_FONT1,FS_NORMAL);
    $t1->SetBox("white", "black", 'gray');
    $t1->SetColor("black");
    $graph->AddText($t1);

    $graph->Stroke();
}
?>